package com.lucaswangdev.web.controller;

import com.lucaswangdev.biz.request.workOrder.WorkOrderCreateRequest;
import com.lucaswangdev.biz.request.workOrder.WorkOrderDeleteRequest;
import com.lucaswangdev.biz.request.workOrder.WorkOrderQueryRequest;
import com.lucaswangdev.biz.request.workOrder.WorkOrderUpdateRequest;
import com.lucaswangdev.biz.response.common.PagingResponse;
import com.lucaswangdev.biz.service.WorkOrderService;
import com.lucaswangdev.model.convert.WorkOrderConverter;
import com.lucaswangdev.model.dto.WorkOrderDTO;
import com.lucaswangdev.web.BaseResult;
import com.lucaswangdev.web.vo.WorkOrderVO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/workOrder")
public class WorkOrderController {
    @Resource
    private WorkOrderService workOrderService;

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseResult<Void> insert(@RequestBody WorkOrderCreateRequest request){
        Boolean success = workOrderService.create(request);
        if(success == null || !success) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }

    @RequestMapping(value = "/list-page", method = RequestMethod.POST)
    public BaseResult<PagingResponse<WorkOrderVO>> listPage(@RequestBody WorkOrderQueryRequest request){
        PagingResponse<WorkOrderDTO> response = workOrderService.listPage(request);
        if(response == null) {
            return BaseResult.systemFail();
        }
        if(response.isSuccess()){
            List<WorkOrderVO> vos = WorkOrderConverter.INSTANCE.convertDTOList2VOList(response.getTableData());
            PagingResponse<WorkOrderVO> result = PagingResponse.build(vos, response.getPaging());
            return BaseResult.success(result);
        } else {
            return BaseResult.bizFail(response.getErrorMsg());
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Void> update(@RequestBody WorkOrderUpdateRequest request){
        Boolean success = workOrderService.modified(request);
        if(success == null || !success) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Void> delete(@RequestBody WorkOrderDeleteRequest request){
        Boolean success = workOrderService.deleteById(request);
        if(success == null || !success) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }
}
